package obs;

import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class OneLockManyTag {
    public static Integer digital =0;
    public static void main(String[] args) {
//        Integer digital = 0;
        Lock lock = new ReentrantLock();
        Condition tag1 = lock.newCondition();
        Condition tag2 = lock.newCondition();
        Condition tag3 = lock.newCondition();

        new Thread(()->{

            Scanner scanner = new Scanner(System.in);
            while (true){
                String s = scanner.nextLine();
                lock.lock();
                try{
                    digital = Integer.parseInt(s);
                }catch (Exception e){
                    digital=100;
                }
                System.out.println(Thread.currentThread().getName() + " thread: " + digital.toString());

                try{
                    tag1.signal();
                }finally {
                    lock.unlock();
                }


            }


        },"操作人员").start();
        new Thread(() -> {
            final Lock l = lock;
            while (true){
                l.lock();
                try{
                    tag1.await();
                    digital+=4;
                    System.out.println(Thread.currentThread().getName() + " thread: " + digital.toString());
                    tag2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    l.unlock();
                }


            }
        }, "一号加法").start();
        new Thread(() -> {

            final Lock l = lock;
            while (true){
                l.lock();
                try{
                    tag2.await();
                    digital*=10;
                    System.out.println(Thread.currentThread().getName() + " thread: " + digital.toString());
                    tag3.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    l.unlock();
                }


            }
        }, "二号乘法").start();
        new Thread(() -> {

            final Lock l = lock;
            while (true){
                l.lock();
                try{
                    tag3.await();
                    digital-=5;
                    System.out.println(Thread.currentThread().getName() + " thread: " + digital.toString());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    l.unlock();
                }


            }
        }, "三号减法").start();

        while (true) {
            try {
                Thread.sleep(3000);
                lock.lock();
                try{
                    System.out.println(Thread.currentThread().getName() + " thread: " + digital.toString());
                }finally {
                    lock.unlock();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//读写锁　共享读　互斥写
//写锁在外　读锁在内　.内锁会经常发生作用　卡顿　而写锁不会经常发生作用　
//写操作过两道关　读操作只过一道关　就是写锁，而写锁不经常发生所以不用
